package com.ytrsoft.util;

import com.ytrsoft.base.FlatArcBorder;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;

public final class Message {

    private Message() {
        throw new UnsupportedOperationException();
    }

    public static void warn(String message) {
        showCustomDialog(message, "警告", JOptionPane.WARNING_MESSAGE, "确定");
    }

    public static void err(Exception e) {
        showCustomDialog(e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE, "确定");
    }

    public static void err(String message) {
        showCustomDialog(message, "错误", JOptionPane.ERROR_MESSAGE, "确定");
    }

    public static void info(String message) {
        showCustomDialog(message, "信息", JOptionPane.INFORMATION_MESSAGE, "确定");
    }

    private static void showCustomDialog(String message, String title, int messageType, String buttonText) {
        JButton okButton = createCustomButton(buttonText);
        final JOptionPane optionPane = new JOptionPane(message, messageType, JOptionPane.DEFAULT_OPTION, null, new Object[]{okButton}, okButton);
        final JDialog dialog = optionPane.createDialog(title);
        okButton.addActionListener(e -> {
            dialog.dispose();
        });
        dialog.setVisible(true);
    }

    private static JButton createCustomButton(String text) {
        JButton button = new JButton(text);
        button.setBackground(Color.decode("#3e3e3e"));
        button.setForeground(Color.decode("#ffffff"));
        button.setPreferredSize(new Dimension(60, 26));
        button.setFocusable(false);
        return button;
    }
}
